#include "image_server.hpp"
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
#include <any>
#include <variant>
#include <map>

using json = nlohmann::json;

// 辅助函数：将JSON配置转换为std::map<std::string, std::any>
std::map<std::string, std::any> parseConfig(const json& config) {
    std::map<std::string, std::any> result;
    
    // 解析FPS
    result["fps"] = config.value("fps", 30);
    
    // 解析头部摄像头配置
    if (config.contains("head_camera_type")) {
        result["head_camera_type"] = config["head_camera_type"].get<std::string>();
        
        if (config.contains("head_camera_image_shape")) {
            result["head_camera_image_shape"] = std::vector<int>{
                config["head_camera_image_shape"][0].get<int>(),
                config["head_camera_image_shape"][1].get<int>()
            };
        }
        
        if (config.contains("head_camera_id_numbers")) {
            std::vector<std::variant<int, std::string>> ids;
            for (const auto& id : config["head_camera_id_numbers"]) {
                if (id.is_number()) {
                    ids.push_back(id.get<int>());
                } else if (id.is_string()) {
                    ids.push_back(id.get<std::string>());
                }
            }
            result["head_camera_id_numbers"] = ids;
        }
    }
    
    // 解析腕部摄像头配置
    if (config.contains("wrist_camera_type")) {
        result["wrist_camera_type"] = config["wrist_camera_type"].get<std::string>();
        
        if (config.contains("wrist_camera_image_shape")) {
            result["wrist_camera_image_shape"] = std::vector<int>{
                config["wrist_camera_image_shape"][0].get<int>(),
                config["wrist_camera_image_shape"][1].get<int>()
            };
        }
        
        if (config.contains("wrist_camera_id_numbers")) {
            std::vector<std::variant<int, std::string>> ids;
            for (const auto& id : config["wrist_camera_id_numbers"]) {
                if (id.is_number()) {
                    ids.push_back(id.get<int>());
                } else if (id.is_string()) {
                    ids.push_back(id.get<std::string>());
                }
            }
            result["wrist_camera_id_numbers"] = ids;
        }
    }
    
    return result;
}

int main(int argc, char** argv) {
    try {
        // 默认配置
        json config = {
            {"fps", 30},
            {"head_camera_type", "opencv"},
            {"head_camera_image_shape", {480, 640}},
            {"head_camera_id_numbers", {4}},
            {"wrist_camera_type", "opencv"},
            {"wrist_camera_image_shape", {480, 640}},
            {"wrist_camera_id_numbers", {6, 8}}
        };
        
        // 如果提供了配置文件路径，从文件加载配置
        if (argc > 1) {
            std::string config_path = argv[1];
            std::ifstream config_file(config_path);
            if (config_file.is_open()) {
                config_file >> config;
                std::cout << "[Image Server] Loaded configuration from " << config_path << std::endl;
            } else {
                std::cerr << "[Image Server] Failed to open config file: " << config_path << std::endl;
                std::cerr << "[Image Server] Using default configuration." << std::endl;
            }
        } else {
            std::cout << "[Image Server] No config file provided. Using default configuration." << std::endl;
        }
        
        // 解析配置
        auto parsed_config = parseConfig(config);
        
        // 创建并启动图像服务器
        int port = 5555;
        if (argc > 2) {
            port = std::stoi(argv[2]);
        }
        
        bool unit_test = false;
        if (argc > 3 && std::string(argv[3]) == "test") {
            unit_test = true;
        }
        
        ImageServer server(parsed_config, port, unit_test);
        server.sendProcess();
        
        return 0;
    } catch (const std::exception& e) {
        std::cerr << "[Image Server] Exception in main: " << e.what() << std::endl;
        return 1;
    }
}
